#pragma once
#include "EngineObject.h"

namespace DreamEngine 
{
enum TEX_FORMAT
{
	TF_UNKOWN	   = 0,
	TF_R8G8B8      = 1,
	TF_A8R8G8B8    = 2,
	TF_X8R8G8B8    = 3,
	TF_L8          = 4,
	TF_X8L8V8U8    = 5,
};

class _EngineExport CTexture : public CEngineObject
{

public:

	struct TexRect 
	{
		int     pitch;
		void*   pBits;
	};

	CTexture(const CString& name);
	virtual ~CTexture(void);

	virtual Error	Create( TEX_FORMAT		texFormat, 
							m_uint32		width, 
							m_uint32		height, 
							RSPOOL			pool=RSP_MANAGER,
							m_dwrd			usage=0, 
							m_uint16		mipLevel=0);

	virtual Error	CreateFromFile(const CString&	fileName, 
								   TEX_FORMAT		texFormat=TF_UNKOWN, 
								   m_uint32			width=0, 
								   m_uint32			height=0,
								   RSPOOL			pool=RSP_MANAGER,
								   m_dwrd			usage=0, 
								   m_uint16			mipLevel=0);

	virtual void	Release()=0;
	virtual	void	LockTexBuf(TexRect* pTexRect, int mipLevel=0)=0;
	virtual void	UnLockTexBuf(int mipLevel=0)=0;
	virtual void*	GetTextureBuf(void)const=0;

	CColor			GetPixelColor(int x, int y);
	void			GetPixelColor(MATH_OUT CColor& color, int x, int y);
	void			SetPixelColor(int x, int y, const CColor& color);

	//void			CopyFrom(CRenderSystem* pRenderSystem, CTexture* pSourceTexture);
	//void			SaveToFile(const std::string& fileName, D3DXIMAGE_FILEFORMAT fileFormat=D3DXIFF_DDS);
	

	const CString&	GetName(void)const			{ return m_name;}

	m_uint32		GetWidth(void)const			{return m_width;}
	m_uint32		GetHeight(void)const		{return m_height;}
	m_uint16		GetMipLevel(void)const		{return m_mipLevels;}
	TEX_FORMAT		GetFormat(void)const		{return m_format;}
	   

	static	int		CaculatePixelByte(CTexture* pTexture);

protected:

	virtual Error	_CreateTexture( TEX_FORMAT		texFormat, 
									m_uint32		width, 
									m_uint32		height, 
									RSPOOL			pool=RSP_MANAGER,
									m_dwrd			usage=0, 
									m_uint16		mipLevel=0)=0;		

	virtual Error	_CreateTextureFromFile( const CString&	fileName, 
											TEX_FORMAT		textureFormat, 
											m_uint32		width=0, 
											m_uint32		height=0,
											RSPOOL			pool=RSP_MANAGER,
											m_dwrd			usage=0, 
											m_uint16		mipLevel=0)=0;
	
protected:

	CString				m_name;

	m_uint32			m_width;
	m_uint32			m_height;
	m_uint16			m_mipLevels;
	m_dwrd				m_usage;
	RSPOOL				m_pool;

	TEX_FORMAT			m_format;

	TexRect				m_texRect;
	
};

}

